// 位置搜索模块
var map = new AMap.Map('mpabox', {
  resizeEnable: true,
  zoom: 11,
});
var search = {
    el: "#uppage_special",
    object: "",
    area: "#area",
    input: "#search",
    list: ".search-ul",
    val: "",
    current:"",

    // 根据城市、关键字获取地址列表
    getResult: function(){
        var _this = this;
        $(_this.list).empty();
        AMap.plugin('AMap.Autocomplete', function(){
            var chooseCity = $(_this.area).find("option:selected").text();
            if(chooseCity == "香港") chooseCity = "香港特别行政区";
            var autoOptions = {
              city: chooseCity,
              citylimit: true
            }
            var autoComplete = new AMap.Autocomplete(autoOptions);
            autoComplete.search(_this.val, function(status, result) {
                console.log(result);
                if (result.info === 'OK') {
                    $.each(result.tips,function(index,object){
                        // 筛选具有id、坐标、与城市相符的位置
                        if(object.id != "" && object.location != undefined){
                            $(_this.list).append(
                                '<li lng="'+ object.location.lng +'" lat="'+ object.location.lat +'">'+
                                    '<span>'+ object.name +'</span>'+
                                    '<em>'+ object.district+object.address +'</em>'+
                                '</li>'
                            );
                        }
                    });
                    $(_this.list).find("li").bind("click",function(){
                        _this.clickPlace(this);
                    });
                }
            })
        })
    },

    // 点击地址
    clickPlace: function(el){
        var name = $(el).find("span").text(),
            district = $(el).find("em").text(),
            lng = $(el).attr("lng"),
            lat = $(el).attr("lat");
        if(name == "当前位置"){
            var address = this.current.addressComponent;
            name = address.province + address.city + address.district + address.street;
        }
        $(this.object)
            .attr("lng",lng)
            .attr("lat",lat)
            .attr("district",district)
            .val(name);
        this.hide();
    },

    // 显示
    show: function(object){
        this.object = "#"+object;
        $(this.el).show();
    },

    // 隐藏
    hide: function(){
        var _this = this;
        this.val = "";
        $(this.input).val("");
        $(this.area).find('option').removeAttr("selected");
        $(this.area).trigger("change");
        $(this.el).addClass("hide");
        setTimeout(function(){
            $(_this.el).removeClass("hide").hide();
        },500);
    },

    // 初始化
    init: function(){
        var _this = this;
        var center = map.getCenter();

        // 当前位置信息
        AMap.service('AMap.Geocoder',function(){
            geocoder = new AMap.Geocoder({
                city: ""
            });
            geocoder.getAddress(center, function(status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    console.log(result);
                    _this.current = result.regeocode;
                    $(".current-location").html(
                        '<span>当前位置</span>'+
                        '<em>'+ result.regeocode.formattedAddress +'</em>'
                    )
                    .attr("lng",center.lng)
                    .attr("lat",center.lat)
                    .bind("click",function(){
                        _this.clickPlace(this);
                    });
                }else{
                    console.log("当前位置获取失败");
                }
            });
        })

        // 搜索框绑定事件
        $(_this.input).bind('input propertychange', function() {
            var searchStr = $(this).val();
            _this.val = searchStr;
            if( searchStr != "" && searchStr != null && searchStr != undefined ){
                _this.getResult();
            }
            else{
                $(_this.list).empty();
            }
        });

        // 城市切换时查询
        $(_this.area).change(function(){
            _this.getResult();
        });

        // 关闭窗口
        $(this.el).find(".close").bind("click",function(){
            _this.hide();
        });
    }
};

search.init();